<?php

class M_public_function extends CI_Model{
	
	function M_public_function(){
		parent::__construct();
	}
	
	function clear_cache(){
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
		$this->output->set_header("Pragma: no-cache");
	}
	
	function string_limit_words($string, $word_limit) {
		$words = explode(' ', $string);
		return implode(' ', array_slice($words, 0, $word_limit));
	}
	
	function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'){
		$str = '';
		$count = strlen($charset);
		while ($length--) {
			$str .= $charset[mt_rand(0, $count-1)];
		}
		return $str;
	}
	
	
	function get_root_parent($menu_id) {
		$result = 0;
		do{
			$sql = "SELECT * FROM s_menus WHERE menu_id=".$menu_id;
			$rs = $this->db->query($sql);
			$row = $rs->row_array();
				
			$menu_id = $row['menu_parent'];
			$result = $row['menu_id'];
				
		}while($menu_id != 0);
	
		return $result;
	
	}
	
	function menus(){
		if($this->session->userdata('group_id') > 0){
			$sql = "SELECT  CONCAT(REPEAT('    ', level - 1), CAST(hi.menu_id AS CHAR)) AS treeitem, menu_parent, level, 
				hi.menu_title, hi.menu_link, hi.menu_id,
				s_permissions.perm_group AS perm_group,
				s_permissions.perm_menu AS perm_menu,
				s_permissions.perm_priv AS perm_priv,
				s_usergroups.group_id AS group_id,
				s_usergroups.group_name AS group_name,
				s_usergroups.group_desc AS group_desc,
				s_usergroups.group_active AS group_active
			FROM    (
					SELECT  hierarchy_connect_by_parent_eq_prior_id(menu_id) AS menu_id, @level AS level
					FROM    (
							SELECT  @start_with := 0,
									@menu_id := @start_with,
									@level := 0
							) vars, s_menus
					WHERE   @menu_id IS NOT NULL
					) ho
			JOIN    s_menus hi
			ON      hi.menu_id = ho.menu_id
			JOIN 		s_permissions
			ON			s_permissions.perm_menu = ho.menu_id
			JOIN 		s_usergroups
			ON			s_usergroups.group_id = s_permissions.perm_group
			WHERE		s_usergroups.group_id = ".$this->session->userdata('group_id');
		}else{
			$sql = "SELECT  CONCAT(REPEAT('    ', level - 1), CAST(hi.menu_id AS CHAR)) AS treeitem, menu_parent, level, hi.menu_title, hi.menu_link, hi.menu_id
			FROM    (
					SELECT  hierarchy_connect_by_parent_eq_prior_id(menu_id) AS menu_id, @level AS level
					FROM    (
							SELECT  @start_with := 0,
									@menu_id := @start_with,
									@level := 0
							) vars, s_menus
					WHERE   @menu_id IS NOT NULL
					) ho
			JOIN    s_menus hi
			ON      hi.menu_id = ho.menu_id";
		}
		
		/*$sql = "SELECT * FROM (SELECT menu_id,
				menu_parent,
				menu_position,
				menu_title,
				menu_link
			FROM s_menus
			WHERE menu_aktif='y' AND menu_parent=0
			ORDER BY menu_id ASC) AS parent
			UNION
			SELECT * FROM (SELECT menu_id,
				menu_parent,
				menu_position,
				menu_title,
				menu_link
			FROM s_menus
			WHERE menu_aktif='y' AND menu_parent!=0
			ORDER BY menu_parent ASC, menu_title ASC) AS child";*/
		$rs = $this->db->query($sql);
		return $rs->result_array();
	}
	
	function has_children($rows,$id) {
		foreach ($rows as $row) { //php fatal error allowed memory size [error log]
			if ($row['menu_parent'] == $id)
				return true;
		}
		return false;
	}
	
	function build_menu($rows,$root_parent,$parent=0,$i=0){
		$baseurl = base_url();
		if($i==0){
			$result = "<ul>";
			$i++;
		}else{
			$result = "<ul style='width: 250px;'>";
		}
		
		foreach ($rows as $row){
			if(($this->has_children($rows,$row['menu_id'])===true )){
				$href = "#";
			}else{
				$href = "#";
				//$href = "{$baseurl}index.php?c=c_login";
			}
			if ($row['menu_parent'] == $parent){
				if($row['menu_id']==$root_parent){
					$result.="<li>";
				}else{
					$result.="<li id='{$row['menu_link']}' title='{$row['menu_title']}'>";
				}
				//$result.="<li>";
				
				//if($href == "#"){
					$result.= "{$row['menu_title']}";
				//}else{
					//$result.= "<a href='{$href}'>{$row['menu_title']}</a>";
				//}
				
				if ($this->has_children($rows,$row['menu_id']))
					$result.= $this->build_menu($rows,$root_parent,$row['menu_id'],$i);
				$result.= "</li>";
			}
		}
		$result.= "</ul>";
		
		return $result;
	}
	
	function get_menus($menu_id = 0){
		$root_parent = 1;
		if($menu_id<>0){
			$root_parent = $this->get_root_parent($menu_id);
		}
		$menu = $this->menus();
		$result = $this->build_menu($menu,$root_parent);
		
		return $result;
	}
	
	function get_group_list(){
		$query = "SELECT group_id, group_name
		FROM s_usergroups
		WHERE group_active='Y'";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
	
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[$i] = $row;
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function get_user_list(){
		$query = "SELECT user_id, user_name, user_aktif, group_id, group_name
		FROM s_users JOIN s_usergroups ON(s_usergroups.group_id = s_users.user_groups)
		WHERE user_aktif='Y'";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
	
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[$i] = $row;
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function get_unitkerja_list(){
		$query = "SELECT KODEUNIT, NAMAUNIT
		FROM unitkerja";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
	
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[$i] = $row;
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function cbget_unitkerja_list(){
		/*$query = "SELECT KODEUNIT, NAMAUNIT
		FROM unitkerja";*/
		$query = "SELECT CONCAT( REPEAT('-', COUNT(parent.NAMAUNIT) - 1), node.NAMAUNIT) AS NAMAUNIT, node.KODEUNIT AS KODEUNIT
				FROM unitkerja AS node,
				unitkerja AS parent
				WHERE node.LFT BETWEEN parent.LFT AND parent.RGT
				GROUP BY node.KODEUNIT
				ORDER BY node.LFT";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
		
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[$i] = $row;
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	
}

?>